package com.srzk.youthleagueback.mapper;

import com.srzk.youthleagueback.pojo.Sport;
import com.srzk.youthleagueback.pojo.ThematicEducation;
import com.srzk.youthleagueback.utils.ObjectUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

public class SportProvider extends SQL {

    private static final String TABLE_NAME = "sport";

    public String updateSport(@Param("sport") Sport sport) {
        return new SQL() {{
            UPDATE(TABLE_NAME);
            if (!StringUtils.isEmpty(sport.getUser())) {
                SET("user = #{user}");
            }
            if (!StringUtils.isEmpty(sport.getYear())) {
                SET("year= #{year}");
            }
            if (!StringUtils.isEmpty(sport.getId().toString())) {
                SET("id= #{id}");
            }
            if (!ObjectUtils.isEmpty(sport.getDate())) {
                SET("date= #{date}");
                SET("address= #{address}");
                SET("form = #{form}");
                SET("theme= #{theme}");
                SET("content= #{content}");
                SET("count= #{count}");

            }
            WHERE("id = #{id}");
            WHERE("user = #{user}");
            WHERE("year= #{year}");
        }}.toString();
    }

    public String insertSport(@Param("sport") Sport sport) {
        return new SQL() {{
            INSERT_INTO(TABLE_NAME);
            try {
                Map<String, Object> data = ObjectUtil.reflect(sport);
                data.forEach((key, value) -> {
                    if (value != null) {
                        VALUES(key, "\'"+value.toString()+"\'");
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }}.toString();
    }


}
